package org.skywind.Index.HitMappers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.skywind.Index.IndexManager;
import org.skywind.Robot.SpiderBase;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.skywind.Robot.pageRobots.TextTagParser;
import org.skywind.WindSearch.Beans.WebBean;
import org.skywind.tools.Log;

public class MapWeb extends AbstractMapHits {
	
	public MapWeb(){
		this.maxResult = 85;
	}
	public List mapping(MapEvent me) {
		Hits hits = me.getHits();
		Query query = me.getQuery();
		IndexManager im = me.getIndex();
		SpiderBase sb = me.getSpiderbase();
		long maxBollot = 0;
		List list = new ArrayList();
		for (int i = 0; i < hits.length(); i++)
			try {
				Document doc = hits.doc(i);
				WebBean web = new WebBean();
				web.setTitle(highlight(query, TextTagParser.TITLE,
						doc.get(TextTagParser.TITLE), im.getAnalyzer()));
				web.setUrl(doc.get(TextTagParser.URL));
				web.setTime(new Date(Long
						.parseLong(doc.get(TextTagParser.TIME))));
				web.setSize(Long.parseLong(doc.get(TextTagParser.SIZE)));
				web.setContent(highlight(query,
						TextTagParser.CONTENT, doc.get(TextTagParser.CONTENT),
						im.getAnalyzer()));
				web.setSourceid(hits.id(i));
				web.setSourceIP(sb.getRobotManager().getDataHost());
				long bollot = 0;
				try {
					bollot = Long.parseLong(doc.get(TextTagParser.BOLLOT));
				} catch (Exception e) {
				}
				web.setBollot(bollot);
				list.add(web);
				if (bollot > maxBollot)
					maxBollot = bollot;
				if (maxResult > 0 && i >= maxResult)
					break;
			} catch (Exception e) {
				Log.println("[SpiderBase] WebHitMapping: Exception => " + e.getMessage());
			}
		for (int i = 0; i < list.size(); i++) {
			WebBean web = (WebBean) list.get(i);
			double rank = 0;
			rank += (double) i / hits.length() * 0.3;
			rank += (double) web.getBollot() / maxBollot * 0.7;
			web.setRank(rank);
		}
		return list;
	}

}
